package dao;

import org.hibernate.Query;
import org.hibernate.Session;

import dominio.OrdenCompra;

public class OrdenCompraDAO {
	private static OrdenCompraDAO instancia = null;

	private static String query_buscarOrdenCompra = "from OrdenCompra ocompra where ocompra.id = (:id)";

	public static OrdenCompraDAO getInstance() {
		if (instancia == null)
			instancia = new OrdenCompraDAO();
		return instancia;
	}

	private OrdenCompraDAO() {}

	public int guardarOrdenCompra(OrdenCompra orden) {
		Session s = HibernateDAO.getInstance().getSession();
		s.beginTransaction();
		s.saveOrUpdate(orden);
		s.getTransaction().commit();
		return orden.getId();
	}

	public OrdenCompra buscarOrdenCompra(int id) {
		Session s = HibernateDAO.getInstance().getSession();
		Query query = s.createQuery(query_buscarOrdenCompra);
		query.setParameter("id", id);
		query.setMaxResults(1);
		return (OrdenCompra) query.uniqueResult();
	}
}